package top100;

/**
 * @author Godc
 * @description
 */
public class Search {
    public static void main(String[] args) {
        SearchSolution s = new SearchSolution();
        int search = s.search(new int[]{1}, 0);
        System.out.println(search);
    }
}
class SearchSolution {
    public int search(int[] nums, int target) {
        int i = 0,j = nums.length-1;
        while(i<=j){
            int mid = (j-i)/2+i;
            if(nums[mid]==target) return mid;
            else if(nums[mid]<nums[j]){
                if(target==nums[mid]) return mid;
                if(target==nums[j]) return j;
                // 右半边有序
                if(target>nums[mid]&&target<nums[j]){
                    i = mid;
                }else{
                    j = mid-1;
                }
            }else if(nums[mid]>nums[j]){
                // 左半边有序
                if(target==nums[mid]) return mid;
                if(target==nums[j]) return j;
                // 右半边有序
                if(target>nums[mid]&&target<nums[j]){
                    j = mid-1;
                }else{
                    i = mid;
                }
            }
        }
        return -1;
    }
}